/*
 * Created by JFormDesigner on Fri Jun 28 09:31:13 CST 2024
 */

package scct.chenlixian.ui;

import scct.chenlixian.dao.UserDao;
import scct.chenlixian.model.USer;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author DELL
 */
public class DlgUserUpdate extends JDialog {
    int id;

    public DlgUserUpdate(Window owner, int id) {
        super(owner);
        this.id = id;
        initComponents();
        initView();

    }

    private void initView() {
        UserDao userDao = new UserDao();
        USer user = userDao.getUserById(id);
        txtUser.setText(user.getUsername());
        txtPass.setText(user.getPassword());
        txtMobile.setText(user.getMobile());
        txtAge.setValue(user.getAge());
        if (user.getGender().equals("男")) {
            radMale.setSelected(true);
        } else {
            radFemale.setSelected(true);
        }
    }


    private void button1(ActionEvent e) {
        //前端验证
        String username = txtUser.getText();
        String password = txtPass.getText();
        String mobile = txtMobile.getText();
        int age = Integer.parseInt(txtAge.getValue().toString());
        String gender = radMale.isSelected() ? "男" : "女";
        if (!username.matches("^\\w{3,10}$") || !password.matches("^\\w{3,10}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法用户民和密码");
            return;
        }
        if (!mobile.matches("^1[3-9]\\d{9}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法手机号码");
            return;
        }
        if (age < 10 || age > 100) {
            JOptionPane.showMessageDialog(this, "请输入有效的年龄（10—100）");
            return;
        }
        //验证用户名重复
        UserDao userDao = new UserDao();
        //if (userDao.isDupname(username));


        if (userDao.updateUser(id, password, gender, age, mobile)) {
            JOptionPane.showMessageDialog(this, "用户修改成功");
            dispose();
        }
    }

    private void button2(ActionEvent e) {
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - CLX
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        txtUser = new JTextField();
        txtPass = new JTextField();
        txtMobile = new JTextField();
        txtAge = new JSpinner();
        radFemale = new JRadioButton();
        radMale = new JRadioButton();
        button1 = new JButton();
        button2 = new JButton();
        buttonBar = new JPanel();
        okButton = new JButton();

        //======== this ========
        setTitle("\u7528\u6237\u4fee\u6539");
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(
                    0, 0, 0, 0), "JFor\u006dDesi\u0067ner \u0045valu\u0061tion", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder
                    .BOTTOM, new java.awt.Font("Dia\u006cog", java.awt.Font.BOLD, 12), java.awt.Color.
                    red), dialogPane.getBorder()));
            dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                @Override
                public void propertyChange(java.
                                                   beans.PropertyChangeEvent e) {
                    if ("bord\u0065r".equals(e.getPropertyName())) throw new RuntimeException();
                }
            });
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- label1 ----
                label1.setText("\u7528\u6237\u540d");
                contentPanel.add(label1);
                label1.setBounds(new Rectangle(new Point(65, 30), label1.getPreferredSize()));

                //---- label2 ----
                label2.setText("\u5bc6\u7801");
                contentPanel.add(label2);
                label2.setBounds(new Rectangle(new Point(65, 65), label2.getPreferredSize()));

                //---- label4 ----
                label4.setText("\u624b\u673a\u53f7");
                contentPanel.add(label4);
                label4.setBounds(60, 105, label4.getPreferredSize().width, 20);

                //---- label5 ----
                label5.setText("\u5e74\u9f84");
                contentPanel.add(label5);
                label5.setBounds(new Rectangle(new Point(65, 135), label5.getPreferredSize()));

                //---- label6 ----
                label6.setText("\u6027\u522b");
                contentPanel.add(label6);
                label6.setBounds(new Rectangle(new Point(65, 180), label6.getPreferredSize()));

                //---- txtUser ----
                txtUser.setEditable(false);
                contentPanel.add(txtUser);
                txtUser.setBounds(115, 25, 155, 30);
                contentPanel.add(txtPass);
                txtPass.setBounds(115, 60, 155, 30);
                contentPanel.add(txtMobile);
                txtMobile.setBounds(115, 95, 155, 30);
                contentPanel.add(txtAge);
                txtAge.setBounds(115, 135, 155, 30);

                //---- radFemale ----
                radFemale.setText("\u5973");
                contentPanel.add(radFemale);
                radFemale.setBounds(210, 180, 85, radFemale.getPreferredSize().height);

                //---- radMale ----
                radMale.setText("\u7537");
                contentPanel.add(radMale);
                radMale.setBounds(new Rectangle(new Point(125, 180), radMale.getPreferredSize()));

                //---- button1 ----
                button1.setText("\u4fee\u6539");
                button1.addActionListener(e -> button1(e));
                contentPanel.add(button1);
                button1.setBounds(new Rectangle(new Point(85, 215), button1.getPreferredSize()));

                //---- button2 ----
                button2.setText("\u53d6\u6d88");
                button2.addActionListener(e -> button2(e));
                contentPanel.add(button2);
                button2.setBounds(new Rectangle(new Point(210, 215), button2.getPreferredSize()));

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for (int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout) buttonBar.getLayout()).columnWidths = new int[]{0, 80};
                ((GridBagLayout) buttonBar.getLayout()).columnWeights = new double[]{1.0, 0.0};

                //---- okButton ----
                okButton.setText("OK");
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - CLX
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JLabel label2;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JTextField txtUser;
    private JTextField txtPass;
    private JTextField txtMobile;
    private JSpinner txtAge;
    private JRadioButton radFemale;
    private JRadioButton radMale;
    private JButton button1;
    private JButton button2;
    private JPanel buttonBar;
    private JButton okButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
